<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Paquetes extends MY_Controller {

	/**
	 * Evita la validacion (enfocado cuando se usa ajax). Ver mas en privilegios_model
	 * @var unknown_type
	 */
	private $excepcion_privilegio = array('ajax_get_siguiente_numero');

	public function _remap($method){
		// $this->carabiner->css(array(
		// 		array('libs/jquery-ui.css', 'screen'),
		// 		array('libs/ui.notify.css', 'screen'),
		// 		array('libs/jquery.treeview.css', 'screen'),
		// 		array('base-panel.css', 'screen')
		// ));
		// $this->carabiner->js(array(
		// 		array('libs/jquery.min.js'),
		// 		array('libs/jquery-ui.js'),
		// 		array('libs/jquery.notify.min.js'),
		// 		array('libs/jquery.treeview.js'),
		// 		array('general/alertas.js')
		// ));

    $this->carabiner->css(array(
        array('bootstrap/bootstrap-simplex.css', 'screen'),
        array('bootstrap/bootstrap-responsive.css', 'screen'),
        array('bootstrap/charisma-app.css', 'screen'),
        array('bootstrap/jquery-ui-1.8.21.custom.css', 'screen'),
        array('bootstrap/fullcalendar.css', 'screen'),
        array('bootstrap/fullcalendar.print.css', 'print'),
        array('bootstrap/chosen.css', 'screen'),
        array('bootstrap/uniform.default.css', 'screen'),
        array('bootstrap/colorbox.css', 'screen'),
        array('bootstrap/jquery.cleditor.css', 'screen'),
        array('bootstrap/jquery.noty.css', 'screen'),
        array('bootstrap/noty_theme_default.css', 'screen'),
        array('bootstrap/elfinder.min.css', 'screen'),
        array('bootstrap/elfinder.theme.css', 'screen'),
        array('bootstrap/jquery.iphone.toggle.css', 'screen'),
        array('bootstrap/opa-icons.css', 'screen'),
        array('bootstrap/uploadify.css', 'screen'),

        array('base-panel.css', 'screen')
    ));
    $this->carabiner->js(array(
        array('bootstrap/jquery-1.7.2.min.js'),
        array('bootstrap/jquery-ui-1.8.21.custom.min.js'),
        array('bootstrap/bootstrap-transition.js'),
        array('bootstrap/bootstrap-alert.js'),
        array('bootstrap/bootstrap-modal.js'),
        array('bootstrap/bootstrap-dropdown.js'),
        array('bootstrap/bootstrap-scrollspy.js'),
        array('bootstrap/bootstrap-tab.js'),
        array('bootstrap/bootstrap-tooltip.js'),
        array('bootstrap/bootstrap-popover.js'),
        array('bootstrap/bootstrap-button.js'),
        array('bootstrap/bootstrap-collapse.js'),
        array('bootstrap/bootstrap-carousel.js'),
        array('bootstrap/bootstrap-typeahead.js'),
        array('bootstrap/bootstrap-tour.js'),
        array('bootstrap/jquery.cookie.js'),
        array('bootstrap/fullcalendar.min.js'),
        array('bootstrap/jquery.dataTables.min.js'),
        /** chart libraries **/
        array('bootstrap/excanvas.js'),
        array('bootstrap/jquery.flot.min.js'),
        array('bootstrap/jquery.flot.pie.min.js'),
        array('bootstrap/jquery.flot.stack.js'),
        array('bootstrap/jquery.flot.resize.min.js'),

        array('bootstrap/jquery.chosen.min.js'),
        array('bootstrap/jquery.uniform.min.js'),
        array('bootstrap/jquery.colorbox.min.js'),
        array('bootstrap/jquery.cleditor.min.js'),
        array('bootstrap/jquery.noty.js'),
        array('bootstrap/jquery.elfinder.min.js'),
        array('bootstrap/jquery.raty.min.js'),
        array('bootstrap/jquery.iphone.toggle.js'),
        array('bootstrap/jquery.autogrow-textarea.js'),
        array('bootstrap/jquery.uploadify-3.1.min.js'),
        array('bootstrap/jquery.history.js'),
        array('bootstrap/charisma.js'),
    ));


		$this->load->model("empleados_model");
		if($this->empleados_model->checkSession()){
			// $this->empleados_model->excepcion_privilegio = $this->excepcion_privilegio;
			// $this->info_empleado = $this->empleados_model->getInfoEmpleado($_SESSION['id_empleado'], true);
			$this->{$method}();
		}else
			redirect(base_url('panel/home/'));
	}

  public function index()
  {
		redirect(base_url('panel/usuarios'));
	}

	/*
 	|	Muestra el Formulario para agregar un Paquete
 	*/
	public function agregar()
	{
		if (isset($_GET['id']))
		{
			// $this->carabiner->css(array(
			// 	array('libs/jquery.msgbox.css', 'screen'),
			// 	array('general/forms.css', 'screen'),
			// 	array('general/tables.css', 'screen')
			// ));
			$this->carabiner->js(array(
				// array('libs/jquery.msgbox.min.js'),
				array('libs/jquery.numeric.js'),
				// array('general/msgbox.js'),
				array('panel/paquetes/paquetes.js')
			));

			$this->load->model("articulos_model");

			$params['seo'] = array(
				'titulo' => 'Agregar Paquete'
			);
			$params['opcmenu_active'] = 'Usuarios'; //activa la opcion del menu

			$this->config_add_paquete();
			if ($this->form_validation->run() == FALSE)
			{
				$params['frm_errors'] = $this->showMsgs(2, preg_replace("[\n|\r|\n\r]", '', validation_errors()));
			}
			else
			{
				$this->load->model('paquetes_model');

				$diff = $this->paquetes_model->diff_two_dates($_POST['ffecha_vencimiento'], date('Y-m-d'));

				if ($diff<=30) {
					$meses = 1;
				}elseif ($diff<=90) {
					$meses = 3;
				}elseif ($diff<=180) {
					$meses = 6;
				}else
					$meses = 12;

				$paquere = array();
				$paquete['data'] = array('id_usuario' => $_GET['id'],
																	'fecha_vencimiento'   => $_POST['ffecha_vencimiento'],
																	'usuarios_permitidos' => $_POST['fusuarios_permitidos'],
																	'tipo'                => $_POST['ftipo'],
																	'costo'								=> $_POST['fcosto'],
																	'meses'								=> $meses,
																	'status'              => '1');

				if ($_POST['ftipo'] == 'segmento')
					$paquete['articulos'] = $_POST['articulos'];

				$res_mdl = $this->paquetes_model->agregar_paquete($paquete);

				if(!$res_mdl['error'])
					redirect(base_url('panel/paquetes/agregar/?'.String::getVarsLink(array('msg')).'&msg=3'));
			}

			$params['articulos'] = $this->articulos_model->get_articulos_tipo();

			if (isset($_GET['msg']))
				$params['frm_errors'] = $this->showMsgs($_GET['msg']);

			$this->load->view('panel/header', $params);
			$this->load->view('panel/general/menu', $params);
			$this->load->view('panel/paquetes/agregar.php', $params);
			$this->load->view('panel/footer');
		}
		else redirect(base_url('panel/usuarios?msg=1'));
	}

	/*
 	|	Muestra el Resumen de paquetes del usuario
 	*/
	public function resumen()
	{
		if (isset($_GET['id']))
		{
			// $this->carabiner->css(array(
			// array('libs/jquery.msgbox.css', 'screen'),
			// array('general/forms.css', 'screen'),
			// array('general/tables.css', 'screen')
			// ));
			$this->carabiner->js(array(
				// array('libs/jquery.msgbox.min.js'),
				array('general/msgbox.js'),
				array('panel/usuarios/usuarios.js')
			));

			$this->load->model('paquetes_model');

			$params['usuario'] = $this->db->query("SELECT u.id_usuario, u.nombre, u.email, u.tipo, u.status, org.nombre as organizacion
                                            FROM usuarios u
                                            LEFT JOIN organizaciones org ON org.id_organizacion=u.id_organizacion
                                            WHERE id_usuario = ".$_GET['id'])->result();

			$params['seo'] = array(
				'titulo' => 'Resumen del usuario '.$params['usuario'][0]->nombre
			);
			$params['opcmenu_active'] = 'Usuarios'; //activa la opcion del menu

			$params['paquetes'] = $this->paquetes_model->get_paquetes_usuario();

			$params['visitas'] = $this->paquetes_model->get_visitas_articulos($_GET['id']);

			if (isset($_GET['msg']))
				$params['frm_errors'] = $this->showMsgs($_GET['msg']);

			$this->load->view('panel/header', $params);
			$this->load->view('panel/general/menu', $params);
			$this->load->view('panel/paquetes/resumen', $params);
			$this->load->view('panel/footer');
		}
		else
			redirect(base_url('panel/articulos/?'.String::getVarsLink(array('msg')).'&msg=1'));
	}

	/*
 	|	Elimina un paquete
 	*/
	public function eliminar()
	{
		if (isset($_GET['idp']))
		{
			$this->load->model('paquetes_model');
			$res_mdl = $this->paquetes_model->eliminar_paquetes();
			if($res_mdl)
				redirect(base_url('panel/paquetes/resumen/?'.String::getVarsLink(array('msg', 'idp')).'&msg=5'));
		}
		else
			redirect(base_url('panel/paquetes/resumen/?'.String::getVarsLink(array('msg', 'idp')).'&msg=1'));
	}

	/*
 	|	Activa un paquete
 	*/
	public function activar()
	{
		if (isset($_GET['idp']))
		{
			$this->load->model('paquetes_model');
			$res_mdl = $this->paquetes_model->activar_paquetes();
			if($res_mdl)
				redirect(base_url('panel/paquetes/resumen/?'.String::getVarsLink(array('msg', 'idp')).'&msg=6'));
		}
		else
			redirect(base_url('panel/paquetes/resumen/?'.String::getVarsLink(array('msg', 'idp')).'&msg=1'));
	}

  /*
 	|	Asigna las reglas para validar un paquete
 	*/
	public function config_add_paquete($accion='agregar')
	{
		$this->load->library('form_validation');
		$rules = array(
							array('field' => 'fusuarios_permitidos',
										'label' => 'Usuarios Permitidos',
										'rules' => 'required|is_natural'),
							array('field' => 'ftipo',
										'label' => 'Tipo',
										'rules' => 'required'),
							array('field' => 'ffecha_vencimiento',
										'label' => 'Fecha de vencimiento',
										'rules' => 'required|max_length[10]'),
							array('field' => 'fip1',
										'label' => 'Ip 1',
										'rules' => 'valid_ip'),
							array('field' => 'fip2',
										'label' => 'Ip 2',
										'rules' => 'valid_ip'),
							array('field' => 'fip3',
										'label' => 'Ip 3',
										'rules' => 'valid_ip'),
							array('field' => 'fip4',
										'label' => 'Ip 4',
										'rules' => 'valid_ip'),
							array('field' => 'fip5',
										'label' => 'Ip 5',
										'rules' => 'valid_ip'),
							array('field' => 'fcosto',
										'label' => 'Costo',
										'rules' => 'numeric')
		);

		if (isset($_POST['ftipo'])) {
			if ($_POST['ftipo'] == 'segmento')
			{
				$rules[] = 	array('field' => 'articulos[]',
													'label' => 'Articulos',
													'rules' => 'required');
			}
		}

		$this->form_validation->set_rules($rules);
	}

	private function showMsgs($tipo, $msg='', $title='Usuarios')
	{
		switch($tipo){
			case 1:
				$txt = 'El campo ID es requerido.';
				$icono = 'error';
				break;
			case 2: //Cuendo se valida con form_validation
				$txt = $msg;
				$icono = 'error';
				break;
			case 3:
				$txt = 'El paquete se agregó correctamente.';
				$icono = 'success';
				break;
			case 4:
				$txt = 'El paquete se modificó correctamente.';
				$icono = 'success';
				break;
			case 5:
				$txt = 'El paquete se eliminó correctamente.';
				$icono = 'success';
				break;
			case 6:
				$txt = 'El paquete se activó correctamente.';
				$icono = 'success';
				break;
		}

		return array(
				'title' => $title,
				'msg' => $txt,
				'ico' => $icono);
	}
}



/* End of file usuarios.php */
/* Location: ./application/controllers/panel/usuarios.php */
